# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @File  : 单一聚合count操作.py
# @Author: dongguangwen
# @Date  : 2025-06-14 18:43
from pymongo import MongoClient

# 连接到MongoDB（默认端口27017）
client = MongoClient('mongodb://root:root123@192.168.1.119:27017/')

# 创建数据库和集合
db = client['learning_mongodb']
collection = db['users']

pipeline = [
    {"$count": "total_users"}
]

result = list(db.users.aggregate(pipeline))
print(result)
print("用户总数:", result[0]["total_users"])

"""
[{'total_users': 10}]
用户总数: 10
"""